import 'package:flutter/material.dart';
import '../../services/ScreenAdapter.dart';
import '../../services/UserInfo.dart';
import '../../services/EventBus.dart';

// 消息弹框
import 'package:fluttertoast/fluttertoast.dart';

class UserPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => UserPageState();
}

class UserPageState extends State<UserPage> {
  bool isLogin = false;
  String userName = '';

  @override
  void initState() {
    super.initState();

    // 判断是否登录，获取用户信息
    this._getUserInfo();

    //监听广播
    eventBus.on<UserUploadEvent>().listen((event){
      print(event.str);
      // 判断是否登录，获取用户信息
      this._getUserInfo();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Color.fromRGBO(240, 240, 240, 1),
        child: ListView(
          children: [
            // 顶部，登录、注册
            Container(
                width: double.infinity,
                height: ScreenAdapter.height(220),
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('images/user_bg.jpg'),
                    fit: BoxFit.fill,
                  ),
                ),
                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.all(10),
                      child: ClipOval(
                        child: Image.asset(
                          'images/user.png',
                          fit: BoxFit.fill,
                          width: ScreenAdapter.height(120),
                          height: ScreenAdapter.height(120),
                        ),
                      ),
                    ),
                    isLogin
                        ? Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('用户名：$userName',
                                  style: TextStyle(color: Colors.white)),
                              Text('普通会员',
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 8))
                            ],
                          )
                        : InkWell(
                      child: Text('登录/注册', style: TextStyle(color: Colors.white)),
                      onTap: (){
                        Navigator.pushNamed(context, '/login');
                      }
                    )
                  ],
                )),

            _rowContent(
              Icons.home, Colors.red, '全部订单',
              cb: (){
                // 跳转到订单列表页
                Navigator.pushNamed(context, '/order');
              }
            ),
            Divider(),
            _rowContent(Icons.home, Colors.green, '待付款'),
            Divider(),
            _rowContent(Icons.home, Colors.orange, '待收货'),
            Divider(thickness: 10, color: Color.fromRGBO(200, 200, 200, 0.2)),
            _rowContent(Icons.home, Colors.blue, '我的收藏'),
            Divider(),
            _rowContent(Icons.home, Colors.black38, '在线客服'),
            Divider(),
            isLogin ? _rowContent(Icons.home, Colors.black38, '退出登录',cb: _loginOut) : Text(''),
            isLogin ? Divider() : Text('')
          ],
        ),
      ),
    );
  }

  Widget _rowContent(icon, iconColor, text, {cb}) {
    return ListTile(
      leading: Icon(icon, color: iconColor),
      title: Text(text),
      onTap: cb,
    );
  }

  // 获取用户信息，判断是否登录
  _getUserInfo() async {
    bool loginState = await UserInfoServices.getUserLoginState();
    List userInfo = await UserInfoServices.getUserInfo();

    print('userInfo === $userInfo');
    this.setState(() {
      isLogin = loginState;
      if(isLogin){
        userName = userInfo[0]['username'];
      } else {
        userName = '';
      }
    });
  }

  _loginOut(){
    UserInfoServices.loginOut();

    // 重新获取登录信息
    this._getUserInfo();

    // 弹框提示
    Fluttertoast.showToast(
      msg: "已退出登录！",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
    );

  }
}
